<?php


namespace app\oa\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class ReadyController extends HomeBaseController
{
    public function index()
    {
        $id = cmf_get_current_admin_id();
        $where = [];
        if ($id != 1) {
            $role = Db::name('role_user')->where(['user_id' => cmf_get_current_admin_id()])->value('role_id');
            switch ($role) {
                case 3:
                    $where['status'] = 0;
                    break;
                case 2:
                    $where['status'] = 1;
                    break;
                case 4:
                    $where['status'] = 2;
            }
        } else {
            $where['status'] = ['in', 0, 1, 2];
        }
        $where['restatus'] = 0;
        $res = Db::name('oa_local')->where($where)->order('date desc')->select()->toArray();
        foreach ($res as $k => &$v) {
            $v['userName'] = Db::name('user')->where(['id' => $v['user']])->value('user_login');
            $v['lname'] = Db::name('engine')->where(['id' => $v['eid']])->value('name');
        }
        $this->assign('data', $res);
        return $this->fetch();
    }

    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('oa_local')->where(['id' => $data['id']])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['lname'] = Db::name('engine')->where(['id' => $res['eid']])->value('name');
        $setd = Db::name('engine')->field('id,name')->select();
        $this->assign('setd', $setd);
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function back()
    {
        $data = $this->request->param();
        $res = Db::name('oa_local')->where(['id' => $data['id']])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['lname'] = Db::name('engine')->where(['id' => $res['eid']])->value('name');
        $setd = Db::name('engine')->field('id,name')->select();
        $this->assign('setd', $setd);
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function doback()
    {
        $role = Db::name('role_user')->where(['user_id' => cmf_get_current_admin_id()])->value('role_id');
        $restatus = 0;
        switch ($role) {
            case 3 :
                $restatus = 1;
                break;
            case 2:
                $restatus = 2;
                break;
            case 4:
                $restatus = 3;
                break;
        }
        $data = $this->request->param();
        if (empty($data['refound'])) {
            $this->error('请填写驳回理由');
        }
        $res = Db::name('oa_local')->where(['id' => $data['id']])->update(['refound' => $data['refound'], 'restatus' => $restatus]);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    /**
     * 通过审核
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function sou()
    {
        $role = Db::name('role_user')->where(['user_id' => cmf_get_current_admin_id()])->value('role_id');
        if(empty($role)){
            $this->error('无权限');
        }
        $status = 0;
        switch ($role) {
            case 3 :
                $status = 1;
                break;
            case 2:
                $status = 2;
                break;
            case 4:
                $status = 3;
                break;
        }
        $data = $this->request->param();
        $res = Db::name('oa_local')->where(['id' => $data['id']])->update(['status' => $status,'updateTime' => time()]);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function lists()
    {
        $data = Db::name('oa_local')->where('status','<>',0)->whereOr('restatus','<>',0)->order('updateTime desc')->select()->toArray();
        foreach ($data as $k => &$v) {
            $v['userName'] = Db::name('user')->where(['id' => $v['user']])->value('user_login');
            $v['lname'] = Db::name('engine')->where(['id' => $v['eid']])->value('name');
        }
        $this->assign('data', $data);
        return $this->fetch();
    }
}